//
//  MSTitleView.m
//  testMasory
//
//  Created by xiangbo on 2018/9/20.
//  Copyright © 2018年 xiangbo. All rights reserved.
//

#import "MSTitleView.h"

#import "MSAllListViewController.h"
// FIXME:
#import "MSHomeFilterViewController.h"

#import "RightImageButton.h"



@interface MSTitleView ()

@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UILabel *subtitleLabel;
@property (nonatomic, strong)RightImageButton *moreButton;

@property (nonatomic, strong) MSFoodData *data; // 数据源

@end

@implementation MSTitleView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews{
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = UIColorHex(0xdddddd);
    [self addSubview:lineView];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.titleLabel.textColor = UIColorHex(0x222222);
    [self addSubview:self.titleLabel];
    self.titleLabel.text = @"这是标题";
    
    self.subtitleLabel = [[UILabel alloc] init];
    self.subtitleLabel.font = [UIFont systemFontOfSize:11.f];
    self.subtitleLabel.textColor = UIColorHex(0x888888);
    [self addSubview:self.subtitleLabel];
    self.subtitleLabel.text = @"这是副标题";
    
    self.moreButton = [[RightImageButton alloc] initWithFrame:CGRectMake(0.f, 0.f, 112, 32)];
    [self.moreButton setTitleColor:UIColorHex(0x222222) forState:UIControlStateNormal];
    self.moreButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.moreButton addTarget:self action:@selector(titleViewMoreButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.moreButton];
    [self.moreButton setTitle:@"查看更多" forState:UIControlStateNormal];
    [self.moreButton setImage:[UIImage imageNamed:@"account_more"] forState:UIControlStateNormal];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(20.f);
        make.right.equalTo(self).offset(-20.f);
        make.top.equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(lineView);
        make.right.equalTo(self).offset(-20.f);
        make.top.equalTo(lineView.mas_bottom).offset(24.5);
    }];
    
    [self.subtitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(lineView);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(4);
    }];
    
    [self.moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-10);
        make.width.mas_equalTo(CGRectGetWidth(self.moreButton.frame) + 25);
        make.centerY.equalTo(self);
    }];
}

#pragma mark - Public Method

- (void)refreshTitle:(MSFoodData *)data {
    self.data = data;
    self.moreButton.hidden = !data.more;
    self.titleLabel.text = [MSSafeObject stringInfo:data.title];
    self.subtitleLabel.text = [MSSafeObject stringInfo:data.subTitle];
}

#pragma mark - Event Response
/** 更多/全部按钮点击 */
- (void)titleViewMoreButtonClick:(UIButton *)sender {
    
    MSAllListViewController *allListVC = [MSAllListViewController loadAllListViewControllerWithFoodData:self.data];
    allListVC.hidesBottomBarWhenPushed = YES;
    UIViewController *currentVC = [UIViewController currentViewController];
    [currentVC.navigationController pushViewController:allListVC animated:YES];
}

@end
